package com.example.tool.erp.demo;

import com.example.tool.erp.sdk.Client;
import com.example.tool.erp.sdk.Pager;
import com.example.tool.erp.sdk.WdtErpException;
import com.example.tool.erp.sdk.api.wms.stockpd.StockPdAPI;
import com.example.tool.erp.sdk.api.wms.stockpd.dto.*;
import com.example.tool.erp.sdk.impl.ApiFactory;
import com.example.tool.erp.sdk.impl.DefaultClient;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.util.Date;

public class StockPd
{
//    public static void main(String[] args) throws WdtErpException
//    {
//        // Client client = DefaultClient.get("wdtapi3", "test", "test");
//        Client client = DefaultClient.get("wdterp30", "http://127.0.0.1:30000/", "POS",
//                "c1fbdd70132de9300d23a05a5f63e150:cc169a95acdaa9ffaed4e9d59f93efaa");
//        StockPdAPI stockPdAPI = ApiFactory.get(client, StockPdAPI.class);
//
//    //    queryTest(stockPdAPI);
//
////        queryDetailTest(stockPdAPI);
////        queryStockPdInWithDetailTest(stockPdAPI);
//        queryStockPdOutWithDetailTest(stockPdAPI);
//    }

    private static void queryTest(StockPdAPI stockPdAPI) throws WdtErpException
    {
        StockPdQueryRequest request = new StockPdQueryRequest();
        Date now = new Date();
        request.setEndTime(DateFormatUtils.format(now, "yyyy-MM-dd HH:mm:ss"));
        request.setStartTime(DateFormatUtils.format(DateUtils.addDays(now, -30), "yyyy-MM-dd HH:mm:ss"));
        StockPdQueryResponse response = stockPdAPI.search(request, new Pager(10, 0, true));

        for (StockPdQueryResponse.Item item : response.getData())
        {
            System.out.println("goods no:" + item.getOrderNo() + " modified : " + item.getModified());

        }
    }
    private static void queryDetailTest(StockPdAPI stockPdAPI) throws WdtErpException
    {//PD2023041906
        StockPdQueryDetailRequest request = new StockPdQueryDetailRequest();
        request.setPdNo("PD2023041906");
        StockPdQueryDetailResponse response = stockPdAPI.search(request, new Pager(10, 0, true));

        for (StockPdQueryDetailResponse.DetailItem item : response.getOrders())
        {
            System.out.println("goods no:" + item.getGoodsNo() + " modified : " + item.getModified());
        }
    }

    private static void queryStockPdInWithDetailTest(StockPdAPI api) throws WdtErpException
    {
        int pageSize = 5;
        Pager pager = new Pager(pageSize, 0, true);
        StockPdInQueryWithDetailRequest request = new StockPdInQueryWithDetailRequest();
        request.setStartTime("2022-03-01");
        request.setEndTime("2022-03-31");

        StockPdInQueryWithDetailResponse response = api.queryStockPdInWithDetail(request, pager);
        Integer total = response.getTotal();
        if (null == total || pageSize >= total)
        {
            System.out.println("处理数据");
            return;
        }

        int totalPage = (total % pageSize == 0 ? total / pageSize : total / pageSize + 1) - 1;
        pager.setCalcTotal(false); //后续翻页不需要计算总条数, 可以大大减少请求时间
        for (int i = totalPage; i >= 0; i--)// 从后向前翻页
        {
            pager.setPageNo(i);
            System.out.print("pager: page_size:" + pageSize + "  page_no: " + i + "  ");
            response = api.queryStockPdInWithDetail(request, pager);
            System.out.println(response.getOrderList());
        }
    }

    private static void queryStockPdOutWithDetailTest(StockPdAPI api) throws WdtErpException
    {
        int pageSize = 5;
        Pager pager = new Pager(pageSize, 0, true);
        StockPdOutQueryWithDetailRequest request = new StockPdOutQueryWithDetailRequest();
        request.setStartTime("2023-03-01");
        request.setEndTime("2023-03-31");

        StockPdOutQueryWithDetailResponse response = api.queryStockPdOutWithDetail(request, pager);
        Integer total = response.getTotal();
        if (null == total || pageSize >= total)
        {
            System.out.println("处理数据");
            return;
        }

        int totalPage = (total % pageSize == 0 ? total / pageSize : total / pageSize + 1) - 1;
        pager.setCalcTotal(false); //后续翻页不需要计算总条数, 可以大大减少请求时间
        for (int i = totalPage; i >= 0; i--)// 从后向前翻页
        {
            pager.setPageNo(i);
            System.out.print("pager: page_size:" + pageSize + "  page_no: " + i + "  ");
            response = api.queryStockPdOutWithDetail(request, pager);
            System.out.println(response.getOrderList());
        }
    }
}
